//package com.yunxin.core.application.service.impl;
//
//import cn.hutool.core.collection.CollUtil;
//import cn.hutool.core.text.CharSequenceUtil;
//import com.fasterxml.jackson.core.type.TypeReference;
//import com.yunxin.common.exception.BusinessException;
//import com.yunxin.common.result.ResultCode;
//import com.yunxin.common.utils.JacksonUtils;
//import com.yunxin.common.utils.SnowflakeKeyGenerator;
//import com.yunxin.core.application.command.popup.AddPopupCommand;
//import com.yunxin.core.application.command.popup.AddProductsCommand;
//import com.yunxin.core.application.command.popup.EditPopupCommand;
//import com.yunxin.core.application.command.popup.SaveProductDataCommand;
//import com.yunxin.core.application.service.PopupApplicationAdminService;
//import com.yunxin.core.domain.model.popup.Popup;
//import com.yunxin.core.domain.model.popup.PopupProductConfig;
//import com.yunxin.core.domain.model.popup.PopupProductConfigRepository;
//import com.yunxin.core.domain.model.popup.PopupRepository;
//import com.yunxin.core.infra.common.popup.PopupStatusEnum;
//import com.yunxin.core.utils.infra.common.constant.CoreResultCode;
//import com.yunxin.core.utils.infra.common.enums.KeyGeneratorPrefixEnum;
//import lombok.RequiredArgsConstructor;
//import org.springframework.stereotype.Service;
//
//import java.util.List;
//import java.util.Objects;
//
///**
// * @author zero°
// * @since 2023-12-11
// */
//@Service
//@RequiredArgsConstructor
//public class PopupApplicationAdminServiceImpl implements PopupApplicationAdminService {
//
//    private final PopupRepository popupRepository;
//
//    private final PopupProductConfigRepository popupProductConfigRepository;
//
//    private final SnowflakeKeyGenerator sequenceGenerator;
//
//    @Override
//    public Boolean add(AddPopupCommand command) {
//        Popup popup = new Popup();
//        popup.setPopupCode(sequenceGenerator.generateKey(KeyGeneratorPrefixEnum.POPUP.getPrefix()));
//        popup.setPopupName(command.getPopupName());
//        popup.setRemark(command.getRemark());
//        popup.setCreator(command.getCreator());
//        Popup popupByName= popupRepository.getByPopupName(command.getPopupName());
//        if(Objects.nonNull(popupByName)){
//            throw new BusinessException(CoreResultCode.DUPLICATE_NAME_EXIST);
//        }
//        return popupRepository.savePopup(popup);
//    }
//
//    @Override
//    public Boolean edit(EditPopupCommand command) {
//        Popup oldPopup = popupRepository.getById(command.getId());
//        if (Objects.isNull(oldPopup)) {
//            throw new BusinessException(ResultCode.DATA_NO_EXIST_ERROR);
//        }
//        Popup popupByName= popupRepository.getByPopupName(command.getPopupName());
//        if(Objects.nonNull(popupByName) && !Objects.equals(popupByName.getId(), oldPopup.getId())){
//            throw new BusinessException(CoreResultCode.DUPLICATE_NAME_EXIST);
//        }
//        Popup popup = new Popup();
//        popup.setPopupName(command.getPopupName());
//        popup.setRemark(command.getRemark());
//        popup.setCreator(command.getEditor());
//        popup.setId(command.getId());
//        popup.setVersion(oldPopup.getVersion() + 1);
//        return popupRepository.updatePopup(popup);
//    }
//
//    @Override
//    public Boolean addProducts(AddProductsCommand command) {
//        Long recommendedPopupId = command.getId();
//        List<Long> productIds = command.getProductIds();
//        Popup oldPopup = popupRepository.getById(command.getId());
//        if (Objects.isNull(oldPopup)) {
//            throw new BusinessException(ResultCode.DATA_NO_EXIST_ERROR);
//        }
//        List<Long> productIdList = CollUtil.newLinkedList();
//        String productData =  popupProductConfigRepository.getLastPopupProductData(recommendedPopupId,null);
//        if (CharSequenceUtil.isNotEmpty(productData)) {
//            productIdList = JacksonUtils.X.parse(productData, new TypeReference<>() {});
//        }
//        assert productIdList != null;
//        productIdList.addAll(productIds);
//        String format = JacksonUtils.X.format(productIdList);
//        PopupProductConfig popupProductConfig = new PopupProductConfig();
//        popupProductConfig.setPopupId(recommendedPopupId);
//        popupProductConfig.setReleaseStatus(PopupStatusEnum.UNPUBLISHED.getValue());
//        popupProductConfig.setProductData(format);
//        popupProductConfig.setCreator(command.getCreator());
//        return popupProductConfigRepository.savePopupProductConfig(popupProductConfig);
//    }
//
//    @Override
//    public Boolean saveProductData(SaveProductDataCommand command) {
//        List<Long> productIds = command.getProductIds();
//        String format = JacksonUtils.X.format(productIds);
//        PopupProductConfig popupProductConfig = new PopupProductConfig();
//        popupProductConfig.setPopupId(command.getId());
//        popupProductConfig.setReleaseStatus(PopupStatusEnum.PUBLISHED.getValue());
//        popupProductConfig.setProductData(format);
//        popupProductConfig.setCreator(command.getEditor());
//        return popupProductConfigRepository.savePopupProductConfig(popupProductConfig);
//    }
//}
